package com.example.demo.controller;

import com.example.demo.mapper.MusicMapper;
import com.example.demo.pojo.Music;
import com.example.demo.util.ReadTxtFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LyricController {
    @Autowired
    MusicMapper musicMapper;

    @RequestMapping(value = "/lyric/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object getLyric(@PathVariable Integer id){
        System.out.println(id);
        Map<Object,Object>map = new HashMap<>();
        Music music = musicMapper.get(id);
        File lyricFile = new File("src"+File.separator+"main"+File.separator+"resources"+File.separator+"static"+File.separator+"lyric"+File.separator+music.getLyricPath());
        String lyric = ReadTxtFileUtil.readTxtFile(lyricFile.getPath());
        if(lyric!=null){
            map.put("code",0);
            map.put("lyric",lyric);
        }
        else{
            map.put("code",1);
        }
        return map;
    }
}
